package org.wordml.web;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.handler.resource.ResourceStreamRequestHandler;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.ContentDisposition;
import org.apache.wicket.util.resource.StringResourceStream;

/**
 * Homepage
 */
public class HomePage extends WebPage {

	private static final long serialVersionUID = 1L;
	private String wordml = "";
	
    /**
	 * Constructor that is invoked when page is invoked without a session.
	 * 
	 * @param parameters
	 *            Page parameters
	 */
    public HomePage(final PageParameters parameters) {

    	Form<Void> frm = new Form<Void>("frm");
    	frm.add(new TextArea<String>("wordmlTextArea", new PropertyModel<String>(this, "wordml")).setRequired(true));
    	frm.add(new SubmitLink("downloadDoc") {
    		@Override
    		public void onSubmit() {
    			super.onSubmit();
    			if(wordml == null){
    				wordml = "";
    			}
    			System.out.println("***************************");
    			System.out.println(wordml);
    			System.out.println("***************************");
    			StringResourceStream stream = new StringResourceStream(wordml,"application/vnd.ms-word");
    			getRequestCycle().scheduleRequestHandlerAfterCurrent(new ResourceStreamRequestHandler(stream)
    			.setFileName("demo.doc")
    			.setContentDisposition(ContentDisposition.ATTACHMENT));
    		}
		});
    	add(new FeedbackPanel("messageArea"));
    	add(frm);
    }
}
